#ifndef ELMESSAGE_H
#define ELMESSAGE_H

#include "../basic/elcomponent.h"
#include "../basic/elicon.h"
#include "../basic/eltheme.h"

class QPropertyAnimation;
class ElTextButton;

/**
 * @brief The ElMessage class
 * Same as ElNotification, don't delete any instance of ElMessage explicitly
 * because instaces would be deleted internally once they fade out.
 * The most recommending way to create instance is to use static function,
 * such as success(), warning(), etc.
 */
class ElMessage : public ElComponent
{
    Q_OBJECT

public:
    enum Type { Success, Warning, Info, Error };

    ///
    /// \brief ElMessage
    /// \param parent: parent should not be null.
    ///
    ElMessage(QWidget *parent);
    ElMessage(QWidget *parent,
              Type type,
              const QString &msg,
              bool isClosable = false,
              bool isCenter = false,
              int duration = 3000,
              int offset = Init_Offset);

    // mostly used for resize event to fix their position.
    static void adjustifyAllPos();

    static void success(QWidget *parent,
                        const QString &msg,
                        bool isClosable = false,
                        bool isCenter = false,
                        int duration = 3000,
                        int offset = Init_Offset);
    static void warning(QWidget *parent,
                        const QString &msg,
                        bool isClosable = false,
                        bool isCenter = false,
                        int duration = 3000,
                        int offset = Init_Offset);
    static void information(QWidget *parent,
                            const QString &msg,
                            bool isClosable = false,
                            bool isCenter = false,
                            int duration = 3000,
                            int offset = Init_Offset);
    static void error(QWidget *parent,
                      const QString &msg,
                      bool isClosable = false,
                      bool isCenter = false,
                      int duration = 3000,
                      int offset = Init_Offset);

    QString message() const;
    void setMessage(const QString &message);

    Type type() const;
    void setType(Type type);

    ElIcon *icon() const;
    void setIcon(ElIcon *icon);
    void setIcon(const QString &fileName);

    int duration() const;
    void setDuration(int duration);

    int offset() const;
    void setOffset(int offset);

    bool isCenter() const;
    void setCenter(bool center);

    bool isClosable() const;
    void setClosable(bool closable);

public slots:
    void slideUp(int distance);
    void fadeIn();
    void fadeOut();

protected:
    void paintEvent(QPaintEvent *e) override;
    void showEvent(QShowEvent *e) override;
    void initPalette() override;

private slots:
    void manager_handle_msgs_animation();

private:
    Q_PROPERTY(QString message READ message WRITE setMessage)
    Q_PROPERTY(Type type READ type WRITE setType)
    Q_PROPERTY(ElIcon *icon READ icon WRITE setIcon)
    Q_PROPERTY(int duration READ duration WRITE setDuration)
    Q_PROPERTY(int offset READ offset WRITE setOffset)
    Q_PROPERTY(bool center READ isCenter WRITE setCenter)
    Q_PROPERTY(bool closable READ isClosable WRITE setClosable)

    static const int Min_Width;
    static const int Icon_Size;
    static const int Spacing;
    static const int Padding_Lr;
    static const int Padding_Tb;
    static const int Init_Offset;

    static QList<ElMessage *> msg_manager;

    using ElComponent::setEnabled;

    QString _msg;
    ElTheme _theme = ElTheme::Information;
    ElIcon *_icon = nullptr;
    int _offset = Init_Offset;
    int _duration = 3000;
    bool _center = false;
    bool _closable = false;

    QTimer *_fadeTimer = nullptr;
    QPropertyAnimation *_anim_pos = nullptr;
    QPropertyAnimation *_anim_opa = nullptr;
    QPropertyAnimation *_icon_anim_opa = nullptr;
    QColor _bdColor = QColor(235, 238, 245);
    ElTextButton *_closeBtn = nullptr;
    qsizetype _no; // the number of this, which starts from 0
    bool _is_repeat_click = false;

    void initAnimation();
    void startIconAnimation(bool isForward = true);
    void refreshPosAnim(int start_pos_y, int end_pos_y);
    void createCloseBtn();
};

#endif // ELMESSAGE_H
